package com.hyz.service.impl;

import client.AssetClient;
import com.hyz.pojo.request.AmUpdateAssetMaintenanceRequest;
import com.hyz.pojo.request.ApprovalResultRequest;
import com.hyz.pojo.request.InsAssetImportRequest;
import com.hyz.pojo.request.InsertAssetOutboundRequest;
import com.hyz.pojo.response.*;
import com.hyz.service.AssetManagerService;
import com.hyz.util.AssetMaintenanceUtils;
import com.hyz.util.AssetSecondmentUtils;
import com.hyz.util.Conversion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.ResponseResult;
import pojo.asset.IsComplete;
import pojo.asset.asset.Asset;
import pojo.asset.assetImport.AssetImport;
import pojo.asset.assetMaintenance.AssetMaintenance;
import pojo.asset.assetMaintenance.AssetMaintenanceResult;
import pojo.asset.assetOutbound.AssetOutbound;
import pojo.asset.assetRetirement.AssetRetirement;
import pojo.asset.assetRetirement.AssetRetirementResult;
import pojo.asset.assetSecondment.AssetSecondment;
import pojo.asset.assetSecondment.AssetSecondmentResult;
import pojo.asset.repository.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author HGD
 * @date 2022/10/8 0:04
 */
@Service
public class AssetManagerServiceImpl implements AssetManagerService {
    @Autowired
    private AssetClient assetClient;
    @Autowired
    private Conversion conversion;
    @Autowired
    private AssetMaintenanceUtils amUtils;
    @Autowired
    private AssetSecondmentUtils asUtils;

    private final int code = 200;

    //------------------------------------------------------------------------------------------------
    // 资产入库

    @Override
    public ResponseResult<List<AssetResponse>> getUnassignedAsset() {
        ResponseResult<List<Asset>> assets = assetClient.getUnassignedAsset();
        if (assets.getStatus() == code) {
            List<AssetResponse> assetResponse = conversion.getAssetResponse(assets.getData());
            return new ResponseResult<>(assets.getStatus(), assets.getMsg(), assetResponse);
        }
        return new ResponseResult<>(assets.getStatus(), assets.getMsg());
    }

    @Override
    public ResponseResult<List<Repository>> getEnableImportRepositories(Long assetId) {
        return assetClient.getEnableImportRepositories(assetId);
    }

    @Override
    public ResponseResult<List<AssetResponse>> insertAssetImport(InsAssetImportRequest request) {
        // TODO: 获取用户id
        Long assetManagerId = 1L;
        ResponseResult<List<Asset>> result = assetClient.insertAssetImport(request.getAssetId(), assetManagerId, request.getRepositoryId());
        if (result.getStatus() != code) {
            return new ResponseResult<>(result.getStatus(), result.getMsg());
        }
        return getUnassignedAsset();
    }

    @Override
    public ResponseResult<List<AssetImportResponse>> amGetAssetImport() {
        // TODO: 获取用户id
        Long assetManagerId = 1L;
        ResponseResult<List<AssetImport>> result = assetClient.amGetAssetImport(assetManagerId);
        if (result.getStatus() != code) {
            return new ResponseResult<>(result.getStatus(), result.getMsg());
        }
        List<AssetImportResponse> responses = conversion.getAssetImportResponse(result.getData());
        return new ResponseResult<>(result.getStatus(), result.getMsg(), responses);
    }

    //------------------------------------------------------------------------------------------------
    // 资产借调

    @Override
    public ResponseResult<List<AssetSecondmentResponse>> amGetAssetSecondments() {
        // TODO: 获取用户id
        Long assetManagerId = 1L;
        ResponseResult<List<AssetSecondment>> result = assetClient.amGetAssetSecondments(assetManagerId);
        return asUtils.getAssetSecondmentResponse(result);
    }

    @Override
    public ResponseResult<List<AssetResponse>> getUnLockAssets(Long assetSecondmentId) {
        ResponseResult<List<Asset>> result = assetClient.getUnLockAssets(assetSecondmentId);
        if (result.getStatus() != code) {
            return new ResponseResult<>(result.getStatus(), result.getMsg());
        }
        List<AssetResponse> assetResponse = conversion.getAssetResponse(result.getData());
        return new ResponseResult<>(result.getStatus(), result.getMsg(), assetResponse);
    }

    @Override
    public ResponseResult<List<AssetSecondmentResponse>> insertAssetOutbound(InsertAssetOutboundRequest request) {
        // TODO: 获取用户id
        Long assetManagerId = 1L;

        ResponseResult<String> result = assetClient.insertAssetOutbound(request.getAssetId(), request.getAssetSecondmentId(), assetManagerId);
        if (result.getStatus() != code) {
            return new ResponseResult<>(result.getStatus(), result.getMsg());
        }
        return amGetAssetSecondments();
    }

    @Override
    public ResponseResult<List<AssetOutboundResponse>> amGetAssetOutbounds() {
        int code = 200;
        // TODO: 获取用户id
        Long assetManagerId = 1L;
        ResponseResult<List<AssetOutbound>> result = assetClient.amGetAssetOutbounds(assetManagerId);
        if (result.getStatus() != code) {
            return new ResponseResult<>(result.getStatus(), result.getMsg());
        }
        List<AssetOutboundResponse> responses = conversion.getAssetOutboundResponse(result.getData());
        return new ResponseResult<>(result.getStatus(), result.getMsg(), responses);
    }

    @Override
    public ResponseResult<List<AssetOutboundResponse>> amUpdateAssetOutbound(ApprovalResultRequest request) {
        ResponseResult<String> result = assetClient.amUpdateAssetOutbound(request.getApprovalId(),
                request.getResult() ? IsComplete.COMPLETE : IsComplete.UNFINISHED);
        if (result.getStatus() != code) {
            return new ResponseResult<>(result.getStatus(), result.getMsg());
        }
        return amGetAssetOutbounds();
    }

    @Override
    public ResponseResult<List<AssetSecondmentResponse>> amUpdateAssetSecondment(ApprovalResultRequest request) {
        ResponseResult<String> responseResult = assetClient.amUpdateAssetSecondment(request.getApprovalId(),
                request.getResult() ? AssetSecondmentResult.ASSET_MANAGER_AGREE : AssetSecondmentResult.FAILED);
        if (responseResult.getStatus() != code) {
            return new ResponseResult<>(responseResult.getStatus(), responseResult.getMsg());
        }
        return amGetAssetSecondments();
    }

    //-----------------------------------------------------------------------
    // 资产维修

    @Override
    public ResponseResult<List<AssetMaintenanceResponse>> amGetAssetMaintenance() {
        // TODO: 获取用户id
        Long assetManagerId = 1L;
        ResponseResult<List<AssetMaintenance>> result = assetClient.amGetAssetMaintenance(assetManagerId);
        if (!Objects.equals(result.getStatus(), code)) {
            return new ResponseResult<>(result.getStatus(), result.getMsg());
        }
        List<AssetMaintenanceResponse> responses = conversion.getAssetMaintenanceResponse(result.getData());
        return new ResponseResult<>(result.getStatus(), result.getMsg(), responses);
    }

    @Override
    public ResponseResult<List<AssetMaintenanceResponse>> amUpdateAssetMaintenance(AmUpdateAssetMaintenanceRequest request) {
        // TODO: 获取用户id
        Long assetManagerId = 1L;
        AssetMaintenanceResult result;
        switch (request.getResult()) {
            case 1:
                result = AssetMaintenanceResult.ASSET_MANAGER_AGREE;
                break;
            case 2:
                result = AssetMaintenanceResult.ASSET_MANAGER_FAILED;
                break;
            case 3:
                result = AssetMaintenanceResult.MAINTENANCE_BY_SUPPLY;
                break;
            case 4:
                result = AssetMaintenanceResult.FAILED;
                break;
            default:
                return new ResponseResult<>(400, "参数错误");
        }
        ResponseResult<Boolean> responseResult = assetClient.amUpdateAssetMaintenance(request.getAssetMaintenanceId(), result, assetManagerId);
        if (!responseResult.getData()) {
            return new ResponseResult<>(responseResult.getStatus(), responseResult.getMsg());
        }
        return amGetAssetMaintenance();
    }

    @Override
    public ResponseResult<Map<String, AssetResponse>> getAssetById(Long assetId) {
        ResponseResult<Asset> result = assetClient.getAssetByAssetId(assetId);
        if (result.getStatus() != code) {
            return new ResponseResult<>(result.getStatus(), result.getMsg());
        }
        Asset asset = result.getData();
        AssetResponse build = AssetResponse.builder()
                .id(asset.getId().toString())
                .assetId(asset.getAssetId())
                .assetName(asset.getAssetName())
                .supplier(asset.getSupplier())
                .life(asset.getLife().toString())
                .status(asset.getStatus().getValue())
                .classes(asset.getClasses().getValue())
                .build();
        Map<String, AssetResponse> map = new HashMap<>();
        map.put("asset", build);
        return new ResponseResult<>(result.getStatus(), result.getMsg(), map);
    }

    //-----------------------------------------------------------------------
    // 资产报废

    @Override
    public ResponseResult<List<AssetRetirementResponse>> amGetAssetRetirement() {
        int code = 200;
        // TODO: 获取用户id
        Long applicantId = 1L;
        ResponseResult<List<AssetRetirement>> result = assetClient.amGetAssetRetirement(applicantId);
        if (result.getStatus() != code) {
            return new ResponseResult<>(result.getStatus(), result.getMsg());
        }
        List<AssetRetirementResponse> responses = conversion.getAssetRetirementResponse(result.getData());
        return new ResponseResult<>(result.getStatus(), result.getMsg(), responses);
    }

    @Override
    public ResponseResult<List<AssetRetirementResponse>> amUpdateAssetRetirement(ApprovalResultRequest request) {
        // TODO: 获取用户id
        Long applicantId = 1L;
        ResponseResult<Boolean> result = assetClient.amUpdateAssetRetirement(request.getApprovalId(),
                request.getResult() ? AssetRetirementResult.ASSET_MANAGER_AGREE : AssetRetirementResult.FAILED,
                applicantId);
        if (!result.getData()) {
            return new ResponseResult<>(result.getStatus(), result.getMsg());
        }
        return amGetAssetRetirement();
    }

    public ResponseResult<List<AssetResponse>> getInventoryAsset(){
        ResponseResult<List<Asset>> assets = assetClient.getInventoryAsset();
        if (assets.getStatus() == code) {
            List<AssetResponse> assetResponse = conversion.getAssetResponse(assets.getData());
            return new ResponseResult<>(assets.getStatus(), assets.getMsg(), assetResponse);
        }
        return new ResponseResult<>(assets.getStatus(), assets.getMsg());
    }

    public ResponseResult<Object> getImportResult(String str){
        ResponseResult<Object> result= assetClient.getExcelAsset(str);
        return result;
    }
}
